import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:openim_common/openim_common.dart';

import './general_settings_logic.dart';

class GeneralSettingsPage extends StatelessWidget {
  final logic = Get.find<GeneralSettingsLogic>();

  GeneralSettingsPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: TitleBar.back(
        title: '通用',
      ),
      backgroundColor: Styles.c_F8F9FA,
      body: SingleChildScrollView(
        child: Column(
          children: [
            10.verticalSpace,
            _buildSettingsGroup([
              _buildSettingItem(
                title: '主题/字体大小',
                onTap: () {
                  // 跳转到主题设置页面
                },
              ),
              _buildDivider(),
              _buildSettingItem(
                title: '暗夜模式',
                onTap: () {
                  // 跳转到暗夜模式设置
                },
              ),
              _buildDivider(),
              _buildSettingItem(
                title: '聊片、音频、文件与通话',
                onTap: () {
                  // 跳转到媒体设置页面
                },
              ),
            ]),
            10.verticalSpace,
            _buildSettingsGroup([
              _buildSettingItem(
                title: '其他',
                showArrow: false,
                titleStyle: Styles.ts_8E9AB0_14sp,
              ),
            ]),
            10.verticalSpace,
            _buildSettingsGroup([
              _buildSettingItem(
                title: '存储空间',
                onTap: () {
                  // 跳转到存储空间管理
                },
              ),
              _buildDivider(),
              _buildSettingItem(
                title: '暗夜记录备份',
                onTap: () {
                  // 跳转到聊天记录备份
                },
              ),
              _buildDivider(),
              _buildSettingItem(
                title: '清空全部聊天记录',
                onTap: () {
                  // 显示确认对话框
                  _showClearChatDialog();
                },
              ),
            ]),
            20.verticalSpace,
          ],
        ),
      ),
    );
  }

  Widget _buildSettingsGroup(List<Widget> children) {
    return Container(
      margin: EdgeInsets.symmetric(horizontal: 16.w),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(8.r),
      ),
      child: Column(children: children),
    );
  }

  Widget _buildSettingItem({
    required String title,
    VoidCallback? onTap,
    bool showArrow = true,
    TextStyle? titleStyle,
  }) {
    return GestureDetector(
      behavior: HitTestBehavior.translucent,
      onTap: onTap,
      child: Container(
        padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 16.h),
        child: Row(
          children: [
            Expanded(
              child: Text(
                title,
                style: titleStyle ?? Styles.ts_0C1C33_17sp,
              ),
            ),
            if (showArrow)
              Icon(
                Icons.arrow_forward_ios,
                size: 16.w,
                color: Styles.c_8E9AB0,
              ),
          ],
        ),
      ),
    );
  }

  Widget _buildDivider() {
    return Divider(
      color: Styles.c_E8EAEF,
      height: 1.h,
      thickness: 1.h,
      indent: 16.w,
      endIndent: 0,
    );
  }

  void _showClearChatDialog() {
    showDialog(
      context: Get.context!,
      builder: (context) => AlertDialog(
        title: Text('清空全部聊天记录'),
        content: Text('确定要清空全部聊天记录吗？此操作不可恢复。'),
        actions: [
          TextButton(
            onPressed: () => Get.back(),
            child: Text('取消'),
          ),
          TextButton(
            onPressed: () {
              Get.back();
              // 执行清空操作
              logic.clearAllChatRecords();
            },
            child: Text('确定', style: TextStyle(color: Colors.red)),
          ),
        ],
      ),
    );
  }
}